package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.model.dto.TeacherDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-30
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {



    // 添加或修改教师
    @Override
    public ResponseResult saveTeacher(TeacherDto dto) {
        LambdaQueryWrapper<CourseTeacher> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseTeacher::getCourseId,dto.getCourseId());
        CourseTeacher teacher = this.getOne(qw);

        CourseTeacher teacher2 = BeanHelper.copyProperties(dto, CourseTeacher.class);
        teacher2.setCompanyId(AuthInfoHolder.getCompanyId());
        teacher2.setCreateDate(new Date());
        teacher2.setCoursePubId(teacher2.getCourseId());

            if (teacher==null){

                this.save(teacher2);
            }else {
                teacher2.setId(teacher.getId());
                this.updateById(teacher2);
            }


        return ResponseResult.okResult();
    }

    // 查询教师
    @Override
    public ResponseResult<CourseTeacher> findTeacher(Long courseId) {

        LambdaQueryWrapper<CourseTeacher> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> list = this.list(qw);

        return ResponseResult.okResult(list);
    }

    // 删除教师
    @Override
    public ResponseResult deleteById(Long id) {
        LambdaQueryWrapper<CourseTeacher> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseTeacher::getCourseId,id);
        this.remove(qw);
        return ResponseResult.okResult();
    }
}
